package com.morty.businessLog.customer;

import com.morty.base.enums.RetEnum;
import com.morty.businessLog.sevice.BusinessLogService;
import com.morty.config.model.TableModifyLog;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Component
public class BusinessLogConsumer {

    @Resource
    private BusinessLogService businessLogService;

    @RabbitListener(queues = "BusinessLogQueue")//监听的队列名称 BusinessLogQueue
    public void process(TableModifyLog tableModifyLog, Message message, Channel channel) throws IOException {
        try {
            channel.basicQos(1);
            int status = businessLogService.addIdxData(tableModifyLog);
            if(RetEnum.SUCCESS.getCode()==status){
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }else{
                log.error("BusinessLogConsumer消息写入ES失败,code:"+status);
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            }
        } catch (Exception e) {
            log.error("BusinessLogConsumer消息处理失败:"+e.toString());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
